
#include <ESP8266WiFi.h>

// 定义RF315接收引脚
#define RF315_RX_PIN D3

volatile unsigned long lastPulseTime = 0;
volatile unsigned int pulseDuration = 0;
volatile bool dataReceived = false;

void setup() {
  Serial.begin(115200);
  pinMode(RF315_RX_PIN, INPUT);
  
  // 设置中断监听RF315信号
  attachInterrupt(digitalPinToInterrupt(RF315_RX_PIN), handleInterrupt, CHANGE);
  
  Serial.println("RF315 Receiver Ready");
}

void loop() {
  if(dataReceived) {
    // 解码接收到的信号
    decodeSignal();
    dataReceived = false;
  }
  delay(10);
}

// 中断处理函数
void ICACHE_RAM_ATTR handleInterrupt() {
  unsigned long currentTime = micros();
  pulseDuration = currentTime - lastPulseTime;
  lastPulseTime = currentTime;
  dataReceived = true;
}

// 信号解码函数
void decodeSignal() {
  // 这里添加你的解码逻辑
  // 315MHz信号通常使用PWM编码
  
  Serial.print("Received pulse duration: ");
  Serial.print(pulseDuration);
  Serial.println(" us");
  
  // 示例：简单解码
  if(pulseDuration > 200 && pulseDuration < 400) {
    Serial.println("Detected short pulse (likely 0)");
  } else if(pulseDuration > 800 && pulseDuration < 1200) {
    Serial.println("Detected long pulse (likely 1)");
  }
}
